/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.common.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.javaeye.common.dto.Module;

public class ModuleDAOHibernate extends HibernateDaoSupport implements ModuleDAO {
	
	@SuppressWarnings("unchecked")
	public List<Module> getModules() {
		return getHibernateTemplate().find("from Module");
	}
	
	public Module getModule(int moduleId) {
		return (Module)getHibernateTemplate().get(Module.class, moduleId);
	}
	
	@SuppressWarnings("unchecked")
	public List<Module> getModules(int parentId) {
		final String sql = "from Module m where m.parentId=? order by m.orderNum";
		return getHibernateTemplate().find(sql, parentId);
	}
	
	@SuppressWarnings("unchecked")
	public Module getModuleCountByAction(String action) {
		final String sql = "from Module m where m.url=?";
		List result = getHibernateTemplate().find(sql, action);
		if (result == null || result.size() == 0) {
			return null;
		}
		return (Module)result.get(0);
	}
}
